<template>
  <div>
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="14" :sm="12">
            <a-form-item label="项目名称">
              <j-input placeholder="" v-model="queryParam.projectName"></j-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="12">
            <a-button type="primary" icon="search" @click="searchQuery">搜索</a-button>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-button type="primary" @click="addUserProject">分配项目</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
      @change="handleTableChange"
    >
      <span slot="action" slot-scope="text, record">
        <a-popconfirm title="确定删除吗?" @confirm="() => doDelete(record.id)">
          <a>删除</a>
        </a-popconfirm>
      </span>
    </a-table>
    <project-assign-modal
      ref="projectAssignForm"
      @projectReload="returnUserProject"
      :tenantId="tenantId"
      :userId="userId"
    ></project-assign-modal>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import ProjectAssignModal from './ProjectAssignModal.vue'
import { api } from './service'

export default {
  name: 'UserProjectPermissionModal',
  mixins: [JeecgListMixin],
  components: {
    ProjectAssignModal
  },
  props: {
    tenantId: {
      type: String,
      default: ''
    },
    userId: {
      type: String,
      default: ''
    }
  },
  watch: {
    tenantId() {
      this.queryParam.tenantId = this.tenantId
      this.debounced()
    },
    userId() {
      this.queryParam.userId = this.userId
      this.debounced()
    }
  },

  data() {
    return {
      disableMixinCreated: true,
      // 表头
      columns: [
        {
          title: '项目ID',
          width: 80,
          dataIndex: 'projectId'
        },
        {
          title: '项目名称',
          dataIndex: 'projectName',
          ellipsis: true
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 100,
          scopedSlots: { customRender: 'action' }
        }
      ],
      dataSource: [],
      url: {
        list: api.userProjList(this.userId),
        delete: ''
      },
      debounced: null
    }
  },
  created() {
    this.queryParam.tenantId = this.tenantId
    this.queryParam.userId = this.userId
    this.debounced = _.debounce(() => {
      this.url.list = api.userProjList(this.userId)
      this.searchQuery()
    }, 250)
    this.debounced()
  },
  methods: {
    addUserProject() {
      this.$refs.projectAssignForm.show(this.tenantId, this.userId)
    },
    returnUserProject() {
      this.queryParam.tenantId = this.tenantId
      this.queryParam.userId = this.userId
      this.debounced()
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      if (selectedRowKeys.length > 0) {
        this.selectedRowKeys = selectedRowKeys
        this.$emit('selectProject', selectionRows[0].projectId)
      }
    },
    doDelete: function(id) {
      this.url.delete = api.delUserProject(this.userId, id)
      this.handleDelete(id)
    },
    cleanRadio() {
      this.selectedRowKeys = []
    }
  }
}
</script>
